import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {ValidateProvider} from "../../providers/validate/validate";
import {StorageProvider} from "../../providers/storage/storage";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/info/step1',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-register',
    templateUrl: 'register.html',
})
export class RegisterPage {
    register2Page: any = "Register2Page";
    regData = {
        user_name: '',
        nickname: '',
        full_name: '',
        anonymity: false,
        sex: 1,
        birthday: new Date().toISOString(),
        id_number: '',
        id_number_img: []
    };
    step = 0;

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public validate: ValidateProvider,
        public storage: StorageProvider,
        public util: UtilsProvider
    ) {
    }

    //判断用户是否登录
    ionViewCanEnter() {
        return this.validate.checkLogin();
    }

    ionViewWillEnter() {
        this.getData();
    }

    getData() {
        let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
        let token = tokenInfo['token'];
        let apiUrl = "/user_info/get_register_info/token/" + token;
        this.httpService.requestData(apiUrl, response => {
            this.regData = response['data'];
            this.step = response['step'];
        })
    }

    uploadImage(type, e) {
        let file = e.target.files[0];
        if (file == undefined) {
            return false;
        }
        let data = new FormData();
        data.append('image', file);
        data.append('type', 'moments');
        data.append('water', 'true');
        this.httpService.uploadImage('/user/upload_file', data, result => {
            if (type == 'front') {
                //正面
                this.regData.id_number_img[0] = result.img_path;
            } else {
                this.regData.id_number_img[1] = result.img_path;
            }
        });
    }

    //提交第一步注册信息
    doPost() {
        //验证
        if(!this.regData.user_name || this.regData.user_name.length != 11) {
            this.util.presentAlert('手机号格式不正确');
            return false;
        }
        if(!this.regData.nickname){
            this.util.presentAlert('昵称不能为空');
            return false;
        }

        if(!this.regData.full_name){
            this.util.presentAlert('姓名不能为空');
            return false;
        }

        if(this.regData.sex != 1 && this.regData.sex != 2){
            this.util.presentAlert('请选择您的性别');
            return false;
        }

        if(this.regData.birthday == ''){
            this.util.presentAlert('请选择您的出生日期');
            return false;
        }

        if(!this.regData.id_number){
            this.util.presentAlert('身份证号不能为空');
            return false;
        }

        if(this.regData.id_number.length != 18){
            this.util.presentAlert('请输入正确的身份证号');
            return false;
        }

        // if(this.regData.id_number_img.length < 2){
        //     this.util.presentAlert('请依次上传身份证的正反面');
        //     return false;
        // }
        if (this.step == 0) {
            this.util.presentConfirm('当前资料只能修改一次，请确认所填信息是否属实', () => {
                let tokenInfo = JSON.parse(this.storage.get('tokenInfo'));
                let token = tokenInfo['token'];
                let apiUrl = "/user/reg_one/token/" + token;
                this.httpService.requestPostData(apiUrl, this.regData, response => {
                    if (response['code'] == 200) {
                        this.navCtrl.push('Register2Page');
                    }
                })
            });
        } else {
            this.navCtrl.push('Register2Page');
        }
    }




}
